<template>
  <div class="login-page">
    <div class="login-container">
      <div class="login-header">
        <h1>欢迎回来</h1>
        <p>登录以访问更多功能</p>
      </div>

      <form class="login-form" @submit.prevent="handleLogin">
        <div class="form-group">
          <label for="username">
            <i class="fas fa-user"></i>
            用户名
          </label>
          <input 
            type="text" 
            id="username" 
            v-model="form.username" 
            placeholder="请输入用户名"
            required
          >
        </div>

        <div class="form-group">
          <label for="password">
            <i class="fas fa-lock"></i>
            密码
          </label>
          <div class="password-input">
            <input 
              :type="showPassword ? 'text' : 'password'" 
              id="password" 
              v-model="form.password" 
              placeholder="请输入密码"
              required
            >
            <button 
              type="button" 
              class="toggle-password"
              @click="showPassword = !showPassword"
            >
              <i :class="showPassword ? 'far fa-eye-slash' : 'far fa-eye'"></i>
            </button>
          </div>
        </div>

        <div class="form-options">
          <label class="remember-me">
            <input type="checkbox" v-model="form.remember">
            <span>记住我</span>
          </label>
          <a href="#" class="forgot-password">忘记密码？</a>
        </div>

        <button type="submit" class="login-btn" :disabled="isLoading">
          <i class="fas fa-spinner fa-spin" v-if="isLoading"></i>
          <span v-else>登录</span>
        </button>

        <div class="divider">
          <span>或</span>
        </div>

        <div class="social-login">
          <button type="button" class="social-btn github">
            <i class="fab fa-github"></i>
            <span>使用 GitHub 登录</span>
          </button>
          <button type="button" class="social-btn wechat">
            <i class="fab fa-weixin"></i>
            <span>使用微信登录</span>
          </button>
        </div>

        <div class="register-link">
          还没有账号？ <a href="#" @click.prevent="$emit('switch-mode', 'register')">立即注册</a>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      form: {
        username: '',
        password: '',
        remember: false
      },
      showPassword: false,
      isLoading: false
    }
  },
  methods: {
    async handleLogin() {
      this.isLoading = true
      try {
        // 模拟登录请求
        await new Promise(resolve => setTimeout(resolve, 1000))
        
        // 这里应该调用实际的登录 API
        // const response = await loginApi(this.form)
        
        // 模拟登录成功
        const userData = {
          id: 1,
          username: this.form.username,
          avatar: null,
          token: 'mock-token'
        }
        
        this.$emit('login-success', userData)
      } catch (error) {
        console.error('Login failed:', error)
        alert('登录失败，请检查用户名和密码')
      } finally {
        this.isLoading = false
      }
    }
  }
}
</script>

<style scoped>
.login-page {
  min-height: calc(100vh - 200px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-container {
  background: var(--card-bg);
  border-radius: 24px;
  padding: 40px;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.05);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.5);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h1 {
  font-size: 2rem;
  color: var(--dark);
  margin-bottom: 10px;
}

.login-header p {
  color: var(--gray);
  font-size: 1rem;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.form-group label {
  color: var(--dark);
  font-size: 0.9rem;
  display: flex;
  align-items: center;
  gap: 8px;
}

.form-group label i {
  color: var(--primary);
}

.form-group input {
  padding: 12px 16px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  border-radius: 12px;
  font-size: 1rem;
  transition: var(--transition);
  background: rgba(255, 255, 255, 0.8);
}

.form-group input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
}

.password-input {
  position: relative;
}

.toggle-password {
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: var(--gray);
  cursor: pointer;
  padding: 4px;
  transition: var(--transition);
}

.toggle-password:hover {
  color: var(--primary);
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.9rem;
}

.remember-me {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--gray);
  cursor: pointer;
}

.remember-me input[type="checkbox"] {
  width: 16px;
  height: 16px;
  border-radius: 4px;
  border: 1px solid var(--gray);
  cursor: pointer;
}

.forgot-password {
  color: var(--primary);
  text-decoration: none;
  transition: var(--transition);
}

.forgot-password:hover {
  color: var(--primary-light);
}

.login-btn {
  background: var(--primary);
  color: white;
  border: none;
  padding: 12px;
  border-radius: 12px;
  font-size: 1rem;
  cursor: pointer;
  transition: var(--transition);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.login-btn:hover:not(:disabled) {
  background: var(--primary-light);
  transform: translateY(-2px);
}

.login-btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

.divider {
  position: relative;
  text-align: center;
  margin: 20px 0;
}

.divider::before,
.divider::after {
  content: '';
  position: absolute;
  top: 50%;
  width: calc(50% - 30px);
  height: 1px;
  background: rgba(0, 0, 0, 0.1);
}

.divider::before {
  left: 0;
}

.divider::after {
  right: 0;
}

.divider span {
  background: var(--card-bg);
  padding: 0 15px;
  color: var(--gray);
  font-size: 0.9rem;
}

.social-login {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.social-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 12px;
  border-radius: 12px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  background: white;
  color: var(--dark);
  font-size: 1rem;
  cursor: pointer;
  transition: var(--transition);
}

.social-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
}

.social-btn.github {
  background: #24292e;
  color: white;
  border: none;
}

.social-btn.wechat {
  background: #07c160;
  color: white;
  border: none;
}

.register-link {
  text-align: center;
  color: var(--gray);
  font-size: 0.9rem;
}

.register-link a {
  color: var(--primary);
  text-decoration: none;
  font-weight: 500;
  transition: var(--transition);
}

.register-link a:hover {
  color: var(--primary-light);
}

@media (max-width: 600px) {
  .login-page {
    padding: 10px;
  }

  .login-container {
    padding: 30px 20px;
    border-radius: 16px;
  }

  .login-header h1 {
    font-size: 1.8rem;
  }

  .form-group input {
    padding: 10px 14px;
    font-size: 0.95rem;
  }

  .login-btn,
  .social-btn {
    padding: 10px;
    font-size: 0.95rem;
  }
}
</style> 